package com.inst.jsoup_html_demo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ViewFlipper;

import com.inst.jsoup_html_demo.R;

/**
 * Created by chenrukang on 2018/1/28.
 */

public class MyViewFlipper extends ViewFlipper implements MyGestureListener.OnFlingListener {
    //手势监听类
    private GestureDetector mGestureDetector = null;

    private OnViewFlipperListener mOnViewFlipperListener = null;

    public MyViewFlipper(Context context) {
        super(context);
    }

    public MyViewFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setOnViewFlipperListener(OnViewFlipperListener mOnViewFlipperListener) {
        this.mOnViewFlipperListener = mOnViewFlipperListener;
        //初始化自定义滑动事件监听器
        MyGestureListener myGestureListener = new MyGestureListener();
        //绑定自定义的滑动监听器
        myGestureListener.setOnFlingListener(this);
        mGestureDetector = new GestureDetector(myGestureListener);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (null != mGestureDetector) {
            return mGestureDetector.onTouchEvent(ev);
        } else {
            return super.onInterceptTouchEvent(ev);
        }
    }

    /**向下一条滑动事件**/
    @Override
    public void flingToNext() {
        if (null != mOnViewFlipperListener) {
            int childCnt = getChildCount();
            Log.e("下一页信息数量",childCnt+"");

//            if (childCnt == 2) {
//                removeViewAt(1);
//            }
//            addView(mOnViewFlipperListener.getNextView(), 1);
            if (0 != childCnt) {
                setInAnimation(getContext(), R.anim.push_in);
                setOutAnimation(getContext(), R.anim.push_out);
                setDisplayedChild(0);
            }
        }
    }

    @Override
    public void startFlipping() {
        super.startFlipping();
    }

    /**向上一条滑动事件**/
    @Override
    public void flingToPrevious() {
        if (null != mOnViewFlipperListener) {
            int childCnt = getChildCount();

//            if (childCnt == 2) {
//                removeViewAt(1);
//            }
            Log.e("上一页信息数量",childCnt+"");
//            addView(mOnViewFlipperListener.getPreviousView(), 0);
            if (0 != childCnt) {
                setInAnimation(getContext(), R.anim.push_right_in);
                setOutAnimation(getContext(), R.anim.push_right_out);
                setDisplayedChild(0);
            }
        }
    }



    /**自定义View变化监听回调接口**/
    public interface OnViewFlipperListener {
        View getNextView();   //获取下一页View
        View getPreviousView();  //获取上一页View
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 执行touch 事件
        super.onTouchEvent(event);
        return this.onTouchEvent(event);
    }

    //这个方法会先执行，当返回为true时，才执行onTouchEvent 方法
    public boolean dispatchTouchEvent(MotionEvent ev){
//        getParent().requestDisallowInterceptTouchEvent(true);//对子View来说，有一种方法可以阻止父层的View截获touch事件，就是调用这个方法
        super.dispatchTouchEvent(ev);
        return true;
    }


}
